<template>
  <div class="app-container">
    <el-row :gutter="24">
      <el-col :span="5" :xs="24">
        <dept-tree v-model="deptId"/>
      </el-col>
      <el-col :span="19" :xs="24">

        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="用户姓名" prop="userId">
            <el-input
              v-model="queryParams.userId"
              placeholder="用户名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="日程标题" prop="scheduleTitle">
            <el-input
              v-model="queryParams.scheduleTitle"
              placeholder="请输入日程标题"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="紧急程度" prop="scheduleLevel">
            <el-select v-model="queryParams.scheduleLevel" placeholder="请选择紧急程度" clearable>
              <el-option
                v-for="dict in dict.type.schedule_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间" prop="scheduleStart">
            <el-date-picker clearable
                            v-model="queryParams.scheduleStart"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="日程状态" prop="scheduleStatus">
            <el-select v-model="queryParams.scheduleStatus" placeholder="请选择日程状态" clearable>
              <el-option
                v-for="dict in dict.type.schedule_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['userSchedule:schedule:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['userSchedule:schedule:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['userSchedule:schedule:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['userSchedule:schedule:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :border="true" v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange">
          <el-table-column label="日程名称" align="center" width="150" :show-overflow-tooltip="true" prop="scheduleTitle" />
          <el-table-column label="执行人" align="center" width="100" :show-overflow-tooltip="true" prop="nickName" />
          <el-table-column label="紧急程度" align="center" prop="scheduleLevel">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.schedule_level" :value="scope.row.scheduleLevel"/>
            </template>
          </el-table-column>
          <el-table-column label="开始时间/截至时间" align="center" prop="scheduleStart" width="300">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.scheduleStart, '{y}-{m}-{d} {h}:{i}') }}-{{ parseTime(scope.row.scheduleEnd, '{y}-{m}-{d} {h}:{i}') }}</span>
            </template>
          </el-table-column>
<!--          <el-table-column label="结束时间" align="center" prop="scheduleEnd" width="180">-->
<!--            <template slot-scope="scope">-->
<!--              <span></span>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="日程状态" align="center" prop="scheduleStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.schedule_status" :value="scope.row.scheduleStatus"/>
            </template>
          </el-table-column>
<!--          <el-table-column label="备注" align="center" prop="remark" />-->
          <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['userSchedule:schedule:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['userSchedule:schedule:remove']"
              >删除</el-button>
              <el-button
                v-if="scope.row.scheduleStatus=='3'"
                size="mini"
                type="text"
                icon="el-icon-search"
                @click="handleGet(scope.row)"
                v-hasPermi="['userSchedule:schedule:remove']"
              >审核</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />


      </el-col>
    </el-row>


    <!-- 添加或修改用户日程对话框 关闭要销毁组件-->
    <el-dialog :title="title" :destroy-on-close="true" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择用户" prop="userId">
          <dept-user-select  v-model="form.userId"  :fetch-options="getOptions"></dept-user-select>
        </el-form-item>
        <el-form-item label="日程标题" prop="scheduleTitle">
          <el-input v-model="form.scheduleTitle" style="width: 220px;" placeholder="请输入日程标题" />
        </el-form-item>
        <el-form-item label="紧急程度" prop="scheduleLevel">
          <el-select v-model="form.scheduleLevel" placeholder="请选择紧急程度">
            <el-option
              v-for="dict in dict.type.schedule_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="日程时间"  prop="scheduleEnd">
          <template>
            <el-date-picker
              v-model="date"
              style="width: 220px;"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="日程审核" :destroy-on-close="true" :visible.sync="openSh" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="审核结果" prop="scheduleLevel">

          <el-select v-model="form.scheduleStatus" placeholder="请选择日程状态" clearable>
            <el-option
              v-for="dict in dict.type.schedule_res"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSh">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getDeptUser, listSchedule, getSchedule, delSchedule, addSchedule, updateSchedule } from "@/api/userSchedule/schedule";
export default {
  name: "Schedule",
  dicts: ['schedule_level', 'schedule_status','schedule_res'],
  data() {
    return {
      openSh:false,
      date:[],
      //部门id
      deptId:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户日程表格数据
      scheduleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        deptId: null,
        scheduleTitle: null,
        scheduleLevel: null,
        scheduleStart: null,
        scheduleEnd: null,
        scheduleStatus: null,
        scheduleConclusion: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "日程所属用户id不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "部门id不能为空", trigger: "blur" }
        ],
        scheduleTitle: [
          { required: true, message: "日程标题不能为空", trigger: "blur" }
        ],
        scheduleLevel: [
          { required: true, message: "紧急程度不能为空", trigger: "change" }
        ],
        scheduleStart: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        scheduleEnd: [
          { required: true, message: "日程时间未选择", trigger: "blur" }
        ],
        scheduleStatus: [
          { required: true, message: "日程状态不能为空", trigger: "change" }
        ],
        scheduleConclusion: [
          { required: true, message: "日程总结不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "更新人不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch:{
    date(newVal){
      this.form.scheduleStart=newVal[0]
      this.form.scheduleEnd=newVal[1]
      console.log(newVal)
    }
  },
  created() {
    this.getList();

  },
  methods: {
    getOptions(){
        return getDeptUser()
    },

    handleSh(){
      updateSchedule(this.form).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.openSh = false;
        this.getList();
      });
    },
    /** 查询用户日程列表 */
    getList() {
      this.loading = true;
      listSchedule(this.queryParams).then(response => {
        this.scheduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scheduleId: null,
        userId: null,
        deptId: null,
        scheduleTitle: null,
        scheduleLevel: null,
        scheduleStart: null,
        scheduleEnd: null,
        scheduleStatus: null,
        scheduleConclusion: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scheduleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.date=[];
      this.open = true;
      this.title = "添加用户日程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.date=[];
      const scheduleId = row.scheduleId || this.ids
      getSchedule(scheduleId).then(response => {
        this.form = response.data;

        this.date.push(this.form.scheduleStart)
        this.date.push(this.form.scheduleEnd)
        this.open = true;
        this.title = "修改用户日程";
      });
    },

    /** 修改按钮操作 */
    handleGet(row) {
      this.reset();
      const scheduleId = row.scheduleId || this.ids
      getSchedule(scheduleId).then(response => {
        this.form = response.data;
        this.openSh = true;
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.scheduleId != null) {
            updateSchedule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchedule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scheduleIds = row.scheduleId || this.ids;
      this.$modal.confirm('是否确认删除用户日程编号为"' + scheduleIds + '"的数据项？').then(function() {
        return delSchedule(scheduleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('userSchedule/schedule/export', {
        ...this.queryParams
      }, `schedule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
